package src;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.awt.Window.Type;
import java.awt.Toolkit;

import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import java.sql.*;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Main {

	private JFrame frmSystel;
//	private Font f; 
	private static DB db;
	private ResultSet resultado;
	private final JPanel contentPanel = new JPanel();
	private JTextField textoDni;
	private JPasswordField textoPass;
	private JLabel lblContrasea;
	private JDialog DNI;
	private JDialog balanza;
	private JTextField textField;
	private int modelo;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args){
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Class.forName("com.mysql.jdbc.Driver");
					db = new DB();
					db.conectar_db();
					Main window = new Main();
					//window.frmSystel.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		Dni();
		Balanza();
		
		frmSystel = new JFrame();
		frmSystel.setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\Mariano\\Fotos y videos\\ferrari_logo_100x90.jpg"));
//		frmSystel.setExtendedState(JFrame.MAXIMIZED_BOTH);
		frmSystel.setTitle("Systel");
		frmSystel.setBounds(100, 100, 467, 318);
		frmSystel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmSystel.getContentPane().setLayout(null);

		JButton btnBorrar = new JButton("Borrar");
		btnBorrar.setFont(UIManager.getFont("Button.font"));
		btnBorrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String query = null;
				String tabla = "usuarios";
				String id = "idUsuarios";
				int registro = 31;
				
				try {
					
					query = "delete from " +tabla +" where " +id +"=" +registro;
					db.modificar_registro_db(query);
					JOptionPane.showMessageDialog(frmSystel, "Se borro correctamente", "Informacion", 0);
					
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(frmSystel, "No se borro correctamente", "Error", 0, null);
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnBorrar.setBounds(133, 60, 89, 23);
		frmSystel.getContentPane().add(btnBorrar);
		
		JButton btnModificar = new JButton("Modificar");
		btnModificar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String query = null;
				String campo = "Nombre";
				String nombre = "'jorge'";
				String id = "idUsuarios";
				int registro = 9;
				
				try {
					
					query = "update usuarios set " +campo +"=" +nombre +" where " +id +"=" +registro;
					db.modificar_registro_db(query);
				
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnModificar.setBounds(133, 94, 89, 23);
		frmSystel.getContentPane().add(btnModificar);
		
		JButton btnInsertar = new JButton("Insertar");
		btnInsertar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String query = null;
				
				try {
					
					query = "insert into usuarios values(null, '11234095', 'Roberto', 'Juarez', '0', 'X')";
					db.modificar_registro_db(query);
					JOptionPane.showMessageDialog(frmSystel, "Datos insertados correctamente");
					
					query = "select last_insert_id()";
					resultado = db.consultar_db(query);
				      ResultSetMetaData rsmd = resultado.getMetaData();					
				      int numberOfColumns = rsmd.getColumnCount();
				      
				      for (int i = 1; i <= numberOfColumns; i++) {
				        if (i > 1) System.out.print(",  ");
				        String columnName = rsmd.getColumnName(i);
				        System.out.print(columnName);
				      }
				      System.out.println("");
				  
				      while (resultado.next()) {
				        for (int i = 1; i <= numberOfColumns; i++) {
				          if (i > 1) System.out.print(",  ");
				          String columnValue = resultado.getString(i);
				          System.out.print(columnValue);
				        }
				        System.out.println("");  
				      }

					
				} catch (SQLException e1) {

					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnInsertar.setBounds(133, 128, 89, 23);
		frmSystel.getContentPane().add(btnInsertar);
		
		String a = null;
		
		String[] InstanciaEnsayoString = new String[4];
		InstanciaEnsayoString[0] = a;

		int b = 0;
		int[] InstanciaEnsayoInt = new int[4];
		InstanciaEnsayoInt[0] = b;
		
	}
	
	public void Dni() {
		
		DNI = new JDialog();
		DNI.setTitle("Ingreso");		
		DNI.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\rantonana\\Documents\\prueba\\iconoSystel.png"));
		DNI.setBounds(100, 100, 703, 473);
		DNI.setResizable(false);
		DNI.getContentPane().setLayout(new BorderLayout());
		//setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		contentPanel.setBackground(new Color(204, 204, 204));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		DNI.getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		DNI.setModal(true);
		JLabel lblIngreseSuDni = new JLabel("Documento");
		lblIngreseSuDni.setHorizontalAlignment(SwingConstants.LEFT);
		lblIngreseSuDni.setFont(new Font("Tahoma", Font.PLAIN, 40));
		lblIngreseSuDni.setBounds(10, 84, 225, 60);
		contentPanel.add(lblIngreseSuDni);
		
		textoDni = new JTextField();
		textoDni.setFont(new Font("Tahoma", Font.PLAIN, 40));
		textoDni.setColumns(10);
		textoDni.setBounds(241, 81, 434, 67);
		
		textoDni.addActionListener(	new ActionListener() {

	        @Override
	        public void actionPerformed(ActionEvent arg0) {
	            // Se transfiere el foco al siguiente elemento.
	            ((JTextField) arg0.getSource()).transferFocus();
	        }
	    });
		
		contentPanel.add(textoDni);
		
		textoPass = new JPasswordField();
		textoPass.setFont(new Font("Tahoma", Font.PLAIN, 40));
		textoPass.setColumns(10);
		textoPass.setBounds(241, 172, 434, 67);
				
		textoPass.addActionListener(	new ActionListener() {

		@Override
			public void actionPerformed(ActionEvent arg0) {
			            // Se transfiere el foco al siguiente elemento.
			        	((JTextField) arg0.getSource()).transferFocus();
			     }
				});
		contentPanel.add(textoPass);
				
		lblContrasea = new JLabel("Contrase\u00F1a");			
		lblContrasea.setHorizontalAlignment(SwingConstants.LEFT);
		lblContrasea.setFont(new Font("Tahoma", Font.PLAIN, 40));
		lblContrasea.setBounds(10, 179, 225, 60);
		contentPanel.add(lblContrasea);
				
		final JButton btnDniAceptar = new JButton("");
		btnDniAceptar.setFocusable(true);
		btnDniAceptar.setContentAreaFilled(false);
		btnDniAceptar.setBorderPainted(false);
		btnDniAceptar.setBorder(null);
		btnDniAceptar.setBounds(158, 314, 374, 100);
		btnDniAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/LoginAzulGrande.png")));
		btnDniAceptar.setPressedIcon(new ImageIcon(testBoton.class.getResource("/img/LoginBlancoGrande.png")));
		btnDniAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if(textoDni.getText().equals(""))
						DNI.dispose();
					else if(!db.consultar_usuario(Integer.parseInt(textoDni.getText()), new String(textoPass.getPassword())))
					{
						System.out.println("Ingreso Incorrecto");
					}
					else System.out.println("Ingreso Correcto"); 
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnDniAceptar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				btnDniAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/LoginVerdeGrande.png")));
				DNI.getRootPane().setDefaultButton(btnDniAceptar);
				
			}
			@Override
			public void mouseExited(MouseEvent e) {
				btnDniAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/LoginAzulGrande.png")));
				DNI.getRootPane().setDefaultButton(null);
			}
		});
		btnDniAceptar.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				btnDniAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/LoginVerdeGrande.png")));
				DNI.getRootPane().setDefaultButton(btnDniAceptar);
			}
			@Override
			public void focusLost(FocusEvent e) {
				btnDniAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/LoginAzulGrande.png")));
				DNI.getRootPane().setDefaultButton(null);
			}
		});
		
		contentPanel.add(btnDniAceptar);
		DNI.setVisible(true);
		
	}
	
	
public void Balanza() {

		balanza = new JDialog();
		final JPanel contentPanel = new JPanel();

			balanza.setTitle("Balanza");
			//setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
			balanza.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\rantonana\\Documents\\prueba\\iconoSystel.png"));
			balanza.setModal(true);
			balanza.setBounds(100, 100, 638, 425);
			balanza.getContentPane().setLayout(new BorderLayout());
			contentPanel.setBackground(new Color(204, 204, 204));
			contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
			balanza.getContentPane().add(contentPanel, BorderLayout.CENTER);
			contentPanel.setLayout(null);
	//----------------------------------------------Boton Aceptar------------------------------------------------------------
		
			final JButton btnBalanzaAceptar = new JButton("");
				btnBalanzaAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						if(textField.getText().equals("")) balanza.dispose();
						else
							try {	
								modelo = -1;
								if(!db.consultar_balanza(Integer.parseInt(textField.getText()), modelo)) {
								//no se encuentra balanza
								System.out.println("No se encuentra balanza");
								modelo = 0;
								}
								else System.out.println("Balanza encontrada");
									
								
							} catch (NumberFormatException | SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
					}
				});
				btnBalanzaAceptar.setFocusable(true);
				btnBalanzaAceptar.setContentAreaFilled(false);
				btnBalanzaAceptar.setBorderPainted(false);
				btnBalanzaAceptar.setBorder(null);
				btnBalanzaAceptar.setBounds(116, 235, 374, 100);
				btnBalanzaAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/AceptarAzulGrande.png")));
				btnBalanzaAceptar.setPressedIcon(new ImageIcon(testBoton.class.getResource("/img/AceptarBlancoGrande.png")));
				contentPanel.add(btnBalanzaAceptar);
				
				btnBalanzaAceptar.addMouseListener(new MouseAdapter() {
					
					public void mouseEntered(MouseEvent arg0) {
						btnBalanzaAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/AceptarVerdeGrande.png")));
						balanza.getRootPane().setDefaultButton(btnBalanzaAceptar);
						
					}
					public void mouseExited(MouseEvent e) {
						btnBalanzaAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/AceptarAzulGrande.png")));
						balanza.getRootPane().setDefaultButton(null);
					}
				});
				btnBalanzaAceptar.addFocusListener(new FocusAdapter() {
					@Override
					public void focusGained(FocusEvent arg0) {
						btnBalanzaAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/AceptarVerdeGrande.png")));
						balanza.getRootPane().setDefaultButton(btnBalanzaAceptar);
					}
					@Override
					public void focusLost(FocusEvent e) {
						btnBalanzaAceptar.setIcon(new ImageIcon(testBoton.class.getResource("/img/AceptarAzulGrande.png")));
						balanza.getRootPane().setDefaultButton(null);
					}
				});
		
	//------------------------------------------------------------------------------------------------------------------------
			{
				textField = new JTextField();
				textField.setFont(new Font("Tahoma", Font.PLAIN, 40));
				textField.setBounds(63, 117, 494, 67);
				textField.addActionListener(	new ActionListener() {

			        @Override
			        public void actionPerformed(ActionEvent arg0) {
			            // Se transfiere el foco al siguiente elemento.
			            ((JTextField) arg0.getSource()).transferFocus();
			        }
			    });
				contentPanel.add(textField);
				textField.setColumns(10);
			}
	//--------------------------------------------------------------------------------------------------------------------
			{
				JLabel lblIngreseElModelo = new JLabel("Ingrese equipo a ensayar");
				lblIngreseElModelo.setFont(new Font("Tahoma", Font.PLAIN, 40));
				lblIngreseElModelo.setHorizontalAlignment(SwingConstants.CENTER);
				lblIngreseElModelo.setBounds(10, 21, 602, 60);
				
				
				contentPanel.add(lblIngreseElModelo);
			}
			balanza.setVisible(true);
			
		}


	
}
	


